<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/27
 * Time: 14:13
 */

namespace app\admin\controller;


use app\Model\AdminModel;
use app\Model\AdminRoleModel;
use app\Model\SystemMenuModel;
use think\Collection;

//角色管理
class Role extends Common
{
    public function index()
    {
       $getRole = AdminRoleModel::paginate();//列出所有角色
       $data['data'] = $getRole;
        $this->getConAndAct('access');
        return view('index',$data);
    }
/*
 * 添加/编辑角色页
 *
 */
    public function add()
    {
        $getData = input('get.');
        $getRole['code']=0;
        $getRole['role_id']='';
        $getRole['role_name']='';
        $getRole['role_desc']='';
        if ($getData){//如果是编辑
           $getRole= AdminRoleModel::find($getData['role_id']);
           if ($getRole->act_list=='all'){
               $this->error('该角色不可编辑');
           }
            $getRole['code']=1;
        }
        $data['getRole']=$getRole;
        //                                  0为未删除，1已删除
        $getSys = SystemMenuModel::where('is_del','=',0)->select();
        //权限组
        $group = array('system'=>'权限管理','content'=>'内容管理','goods'=>'商品中心','member'=>'会员中心',
            'order'=>'订单中心','data'=>'统计报表','Ad'=>'广告管理','pick'=>'自提点管理','counp'=>'促销管理'

        );
        $dataArray = [];//大数组
        foreach($group as $k=>$v){
            $data2 = [];//存放一组权限组
            foreach ($getSys->toArray() as $key=>$value){
                if ($value['group']==$k){
                        $value['check']=0;
                    if ($getData){//如果是编辑
                       $act_list=explode(",",$getRole->act_list);//字符串转数组
                        if (in_array($value['id'],$act_list)){
                            $value['check']=1;
                        }
                    }
                    array_push($data2,$value);
                    $dataArray[$k]=$data2;
                }
            }
        }
        $data['data']=$dataArray;
        $data['group']=$group;
       return view('add',$data);
    }
    /*
     * 添加/编辑角色
     */
    public function addData()
    {
        $postData = input('post.');
        setDatabaseDef(isset($postData['right']),'权限不能为空');
        //把权限用“，”连接
        $actList = implode(",",$postData['right']);
        $insert = [//需要添加的数据
            'role_name'=>$postData['role_name'],
            'role_desc'=>$postData['role_desc'],
            'act_list'=>$actList
        ];
        $where['role_name']=$postData['role_name'];//名字是否重复
        if ($postData['role_id']!=''){
            $where['role_id']=['<>',$postData['role_id']];
        }
        $rolename=AdminRoleModel::where($where)->find();
        if ($rolename){ return retJson('',201,'角色名重复');}
        if ($postData['role_id']!=''){//编辑
            $addRole = AdminRoleModel::update($insert,['role_id'=>$postData['role_id']]);
        }else{//新增
            $addRole = AdminRoleModel::insert($insert);
        }
        return setDatabase($addRole);
    }

    /*
     * 删除角色
     */
   public function del(){
       $postData = input('post.');
       //当角色有管理员使用时无法删除
       $getAdR=AdminModel::where('role_id','=',$postData['id'])->find();
       if ($getAdR){
           return retJson('', 201, '当前角色有管理员使用，请删除该管理员或改变其角色');
       }
       $delRole = AdminRoleModel::destroy($postData['id']);
        return setDatabase($delRole);
   }
}